<template>
  <div>
    <Header>
      <el-button
        size="small"
        type="primary"
        @click="
          dialogFormVisible = true;
          dialogTitle = '新建标签';
        "
        >新建标签</el-button
      >
      <el-button size="small" @click="delAll">批量删除</el-button>
    </Header>
    <Main
      :data="listData"
      :footer-lock="totals > 10"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        width="30"
        :reserve-selection="true"
        type="selection"
      ></el-table-column>
      <el-table-column
        label="门店标签"
        align="center"
        property="name"
      ></el-table-column>

      <el-table-column
        label="使用门店数量"
        align="center"
        property="storenum"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="200"
        class-nme="tableoperation"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="edit(scope.row)">
            编辑
          </el-button>
          <el-button size="mini" type="text" @click="del(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </Main>
    <Footer :total="totals">
      <el-pagination
        :page-size="10"
        layout="prev, pager, next"
        :total="totals"
        @current-change="changes"
      >
      </el-pagination>
    </Footer>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
      width="30%"
    >
      <el-form ref="form" :model="form" :rules="form">
        <el-form-item
          label="标签名称"
          prop="name"
          :rules="{
            required: true,
            message: '请输入门店编号',
            trigger: 'blur'
          }"
        >
          <el-input
            v-model="form.name"
            style="line-height:20px;"
            maxlength="20"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  STORE_TAGLIST,
  SETTAGLIST,
  DELTAGLIST,
  DELALLTAGLIST
} from "../service";

export default {
  data() {
    return {
      allList: [],
      getRowKeys(row) {
        return row.id;
      },
      listData: [],
      totals: 0,
      pages: 1,
      form: {
        id: "",
        name: ""
      },
      dialogFormVisible: false,
      dialogTitle: "新增标签"
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSelectionChange(v) {
      this.allList = v;
    },
    handleClose() {
      this.dialogFormVisible = false;
      this.form.name = "";
      this.form.id = "";
    },
    async delAll() {
      let arr = [];
      await this.allList.forEach(ele => {
        arr.push(ele.id);
      });
      await this.$confirm("是否批量删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          DELALLTAGLIST({
            id: arr.join(",")
          }).then(res => {
            let { code, msg } = res;
            if (code === 200) {
              this.getList();
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        })
        .catch(() => {
          this.$message.info("已取消!");
        });
    },
    async handleSubmit() {
      await this.$refs["form"].validate(valid => {
        if (valid) {
          SETTAGLIST({ id: this.form.id, name: this.form.name }).then(res => {
            let { code, msg } = res;
            if (code === 200) {
              this.dialogFormVisible = false;
              this.form.name = "";
              this.form.id = "";
              this.getList();
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        }
      });
    },
    edit(e) {
      this.dialogTitle = "修改标签";
      this.form.name = e.name;
      this.form.id = e.id;
      this.dialogFormVisible = true;
    },
    changes(page) {
      this.pages = page;
      this.getList();
    },
    async getList() {
      await STORE_TAGLIST({ page: this.pages, pageshow: 10 }).then(res => {
        let { code, data, msg } = res;
        if (code === 200) {
          this.totals = data.count;
          data.list.forEach(ele => {
            if (ele.soft === 1) {
              ele.soft = true;
            } else {
              ele.soft = false;
            }
          });
          this.listData = data.list;
        } else {
          this.listData = [];
          this.$message.error(msg);
        }
      });
    },
    async del(id) {
      await this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          DELTAGLIST({
            id
          }).then(res => {
            let { code, msg } = res;
            if (code === 200) {
              this.getList();
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        })
        .catch(() => {
          this.$message.info("已取消!");
        });
    }
  }
};
</script>
